define(["SelfWinsow"],function(SelfWinsow){
	var pageSelector="#main-page";
	var calculatorFlag=0;
	var outClassArry=new Array("餐饮食品","行车交通","水果","购物","蔬菜","日用品","休闲娱乐","服装饰品","护肤美妆","其他支出");
	var outColor = ["#ff6600","#ff3300","#ffcc00","#00ccff","#66cc33","#00cc00","#cc66ff","#9900ff","#ff3399","yellow"];
	var inClassArry=new Array("赚钱","奖金","投资","红包","其他收入");
	var inColor=["#ff6699","#ff9900","#ff0000","#ff3300","yellow"];
	
	var outObjArry=new Array();
    var inObjArry=new Array();
    var outPctageObjArry=new Array();
    var inPctageObjArry=new Array();
    var winHeight=$(window).height();
 	var winWidth=$(window).width();
 	var myCanvas=document.getElementById("bingtu");
	var ctx=myCanvas.getContext("2d");


	var timer=null;
	var startPoint= 0;//0.25 * Math.PI;//起始点 ;  
//	var totalAmount;
 	var otxt=document.getElementById("login-Password");
 	
 	return commonObj = {
 	//	objAlert:function(){
 	//		alert("jfaihfiae");
 	//	},
 		createObj:function (name, value,color){ 
			var obj = new Object(); //创建对象 
			obj.name = name; 
			obj.value = value; 
			obj.color = color; 
			//	obj.showName = showName; 
			//	obj.showAge = showAge; 
			return obj; //返回对象 
		},
 		loadPage:function(){
 		//	var winHeight=$(window).height();
 		//	var winWidth=$(window).width();
 			var outListHeight=winHeight-132+12+'px';
			$(".outlay-list").css("height",outListHeight);
			//图标自动适应窗口的高度

			// alert(outClassArry.length);
 			for(var i=0;i<outClassArry.length;i++){
 				outObjArry[i]=commonObj.createObj(outClassArry[i], 0,outColor[i]); 
			// 	alert(outObjArry[i].name);
 			}
 			for(var j=0;j<inClassArry.length;j++){
 				inObjArry[j]=commonObj.createObj(inClassArry[j], 0,inColor[i]); 
 			//	alert(inObjArry[j].name);
 			}
 			//登录界面
 			if(document.all){
     		otxt.onpropertychange=function(){
	    		$(".submit-btn").css({"background":"#07a4f1"});  
	    		$(".submit-btn").removeAttr("disabled");
				}
 			}
  			else{
   				otxt.oninput=function(){
			    $(".submit-btn").css({"background":"#07a4f1"});  
			    $(".submit-btn").removeAttr("disabled");
				}
			}

 		},
 		loadChart:function(chartMes){
 			startPoint=0;
			myCanvas.height=winHeight-176-44;
			myCanvas.width=winWidth;
			var totalAmount;
			var len,chartHeight;
			
	//		commonObj.addChartlist();
	      	switch(chartMes){
	      		case 0:CharPMes="总支出";
		      		len=outPctageObjArry.length;
		      		totalAmount=commonObj.twoAfter(commonObj.Sum(0)+'');break;
	      		case 1:CharPMes="总收入";
		      		len=inPctageObjArry.length;
		      		totalAmount=commonObj.twoAfter(commonObj.Sum(1)+'');break;
	      	}
		//	alert(totalAmount);
			if((totalAmount==='0.00')||(totalAmount==='0')){
				ctx.fillStyle = "#6A6A6A"; 
				ctx.font = "bold 15pt Arial";  // 字体大小，样式
				ctx.textAlign = 'center';  // 位置	
				ctx.fillText('报表空空如也', winWidth/2, (winHeight-176-44)/2);
				ctx.fill();
			
				chartHeight=winHeight-176+"px" ;
				$("#pie-chart").css("height",chartHeight);
				//$(".chart-out-list").css("top",chartHeight);
				
			//	console.log('饼图height：'+$("#pie-chart").height());
				
			}
			else {
			chartHeight=winHeight-176-44+"px" ;
			$(".chart-out-list").css("top",chartHeight);
			$("#pie-chart").css("height",chartHeight);
				var i=0;
				ctx.fillStyle = "black"; 
				ctx.font = "bold 10pt Arial";  // 字体大小，样式
				ctx.textAlign = 'center';  // 位置
				var CharPMes;
	      		//  ctx.textBaseline = 'middle';
	      		switch(chartMes){
	      			case 0:CharPMes="总支出";break;
	      			case 1:CharPMes="总收入";break;
	      		}
				ctx.fillText(CharPMes, winWidth/2, (winHeight-176-44)/2);				
				ctx.fillText(totalAmount, winWidth/2, (winHeight-176-44)/2+15);
				ctx.fill();
	        	timer = setInterval(function(){
	        		
	            if(i>=len){
	                clearInterval(timer);
	            }else{
	                commonObj.drawCircle(i,chartMes);
	                i++;              
	            }
	      	  },40);

			}

    	},
 		drawCircle:function(i,chartClass){
 		//	alert(i);
 		
 			var cicleX=winWidth/2;
			var cicleY=(winHeight-176-44)/2;
			var cicleRadius=winWidth/5;
			var endPoint,printPct,printName; 	
			ctx.strokeStyle = outColor[i];
			ctx.fillStyle = outColor[i];
    		ctx.beginPath();  		
    		ctx.lineWidth =cicleRadius*0.7; 
    		switch(chartClass){
    			case 0:endPoint=startPoint+Math.PI*2*outPctageObjArry[i];
    					printPct=Math.floor(outPctageObjArry[i]*100);
    					printName=outObjArry[i].name;
    				break;
    			case 1:endPoint=startPoint+Math.PI*2*inPctageObjArry[i];
    					printPct=Math.floor(inPctageObjArry[i]*100);
    					printName=inObjArry[i].name;
    				break;
    		}
    		ctx.arc(cicleX,cicleY,cicleRadius,startPoint,endPoint,false);
   			ctx.stroke();
   			ctx.closePath();
   			
   			if(endPoint!==startPoint){
   			ctx.beginPath(); 
   			ctx.lineWidth =2 ;
   			
   			var tempX=Math.cos(startPoint)+Math.cos(endPoint);
   			var tempY=Math.sin(startPoint)+Math.sin(endPoint);

   			var tanRad=tempY/tempX;
   			var rad=Math.atan(tanRad);
   			//console.log('原rad；'+rad);	
   			rad=tempX>0?rad:rad+Math.PI;

   			rad=endPoint-startPoint>=Math.PI?rad+Math.PI:rad;

   			//console.log('Math.cos(rad)'+Math.cos(rad));
			var tempLine=Math.cos(rad)>0?10:-10;
   			//ctx.moveTo(cicleX,cicleY);
   			ctx.moveTo(cicleX+cicleRadius*Math.cos(rad),cicleY+cicleRadius*Math.sin(rad));
   			ctx.lineTo(cicleX+cicleRadius*1.5*Math.cos(rad),cicleY+cicleRadius*1.5*Math.sin(rad));
   			ctx.moveTo(cicleX+cicleRadius*1.5*Math.cos(rad),cicleY+cicleRadius*1.5*Math.sin(rad));
   			ctx.lineTo(cicleX+cicleRadius*1.5*Math.cos(rad)+tempLine,cicleY+cicleRadius*1.5*Math.sin(rad));
   			
		  	ctx.font = "bold 8pt Arial";  // 字体大小，样式
			ctx.textAlign = Math.cos(rad)>0?'start':'end';  // 位置
			ctx.fillText(printName,cicleX+cicleRadius*1.5*Math.cos(rad)+tempLine,cicleY+cicleRadius*1.5*Math.sin(rad)-2);
			ctx.fillText(printPct+"%",cicleX+cicleRadius*1.5*Math.cos(rad)+tempLine,cicleY+cicleRadius*1.5*Math.sin(rad)+10);
			
   			ctx.stroke();//画线框
  			ctx.fill();//填充颜色
   			ctx.closePath();
   			
   			} 		
    		startPoint =endPoint; 
    		//alert(startPoint);
 		},
/*		
 		enterDetialPage:function(){
 			$("#index-page").hide('fast',function(){
 				$("#out-detail-page").show();
 			});
 			
 		},
 */		
 		//验证手机号码
		checkPhone:function(){ 
	        phone = document.getElementById('login-Mobile').value;
	  		//  alert(phone);
	   		 if(!(/^1[34578]\d{9}$/.test(phone))){ 
	     	  // alert("手机号码有误，请重填");  
	        	return false; 
	    	} 
	   		else{
	        	return true; 
	    	}
		},
  		
  		testLoginInput:function(){
        //判断密码是否为空
      //  alert(document.getElementById('login-Mobile').value);
     
    		var logMobile=$('#login-Mobile').val();
    		if(logMobile=="")
    		{
    			commonObj.alertGray("账号不能为空"); 
    		}
    		else{
	            if(commonObj.checkPhone()){  
	                //验证手机号正确,验证密码是否为空
	                $("form").submit(); 
	            }  
	            else{    
	            	commonObj.alertGray("账号只支持手机");
	            //验证码不正确,表单不允许提交             
	            } 
       		}
		},
 		isSubmitCallback:function(isSumbit){
			if(isSumbit=="")
			{
				alertGray("账号与密码不匹配");
			}
		},
 
 		alertGray:function (mess){
			$('#overlay').fadeIn('fast', function(){
                $('#box').animate({ 'top': '60px' }, 500);
				$("#mess").html(mess); 
				setTimeout("commonObj.alertGrayclose()",2000);//2秒消失，可以改动
            });
		},
		alertGrayclose:function(){
 			$('#box').animate({ 'top': '-500px' }, 500, function () {
                $('#overlay').fadeOut('fast');
            });
},
		changePageIndex:function(){
			var sum=0;
	 	///	$(tempSelector).hide('fast');
			tempSelector='#'+this.id+'-page';
			if(tempSelector=='#chart-page'){
				//commonObj.loadChart();
				startPoint= 0;//0.25 * Math.PI;
				commonObj.loadChart(0);
			}
			if(pageSelector!==tempSelector){
				$(pageSelector).hide('fast');
				$(tempSelector).show('fast');	
				pageSelector=tempSelector;
			}

		},
		changePage:function(hideName,showName){
			$(hideName).hide('fast',function(){
				 $(showName).show('fast');
			});
			$('.footer').toggle();

			if(showName==='#account-page'){
				$("#del-btn").hide();
				$("#chg-icon").addClass("close-icon");
	       	 	$("#chg-icon").removeClass("left-back-icon");
				$("#chg-title").html("记账");
				$("#screenName").blur(); 
          		$("#screenName").val("0.00"); 
            	$("#calculator").show();
            	$("#remarkInput").blur(); 
           		$("#remarkInput").val("请输入备注信息"); 
            	$("#remarkInput").css("color","#97a1a6");  
            	$(".function-list").find("ul").hide();
            	$("#num-class-icon").find('.icon').css({"background-image":" url('images/out-photos.png')",
                                     "background-repeat":"no-repeat",
                                     "background-size":"194px auto",
                                     "background-position":'-7px -4px'});
            	$("#num-class-p").text('类别');
            	$("#account-out").addClass("class-on");
	       	 	$("#account-in").addClass("class-off");
	        	$("#account-out").removeClass("class-off");
	       		$("#account-in").removeClass("class-on");
	        	$("#account-in-ul").hide('fast',function(){
	        	$("#account-out-ul").show('fast');
	        });
			}
			else if(showName=='#main-page'){
				var tempNum=0;
				var str=$(".outNum").text();
				tempNum=commonObj.Sum(0);
				/*
				$(".outlay-ul-0 .out-li").each(function(){
					tempNum+=parseFloat($(this).find(".outNum").text());});
  				//计算总的数值
  				*/
  				$('.out-today-num').html(commonObj.twoAfter(tempNum+""));

			}



			
		},
		
		changeSinglePage:function(pageName,isshow){
			//alert(isshow);
			isshow?$(pageName).show('fast'):$(pageName).hide('fast');	
		},
		saveMes:function(){	
			
			if($("#screenName").val()=="0.00"){
				commonObj.alertGray("请输入有效金额");
			}
			else if($('#num-class-p').text()=='类别'){
				commonObj.alertGray("请选择类别");
			}
			else{
				if($("#chg-title").text()=="记账"){
					commonObj.addAccountinglist();
				}
				else if($("#chg-title").text()=="账单详情"){
					commonObj.chgAccountinglist();
				}

				commonObj.changePage('#account-page','#main-page');		
			}
			
		},
		saveDetailMes:function(){

		
		},
		changeButton_0:function(){
			//alert(this.id);
	        $(this).addClass("class-on");
	        $(this).siblings().addClass("class-off");
	        $(this).removeClass("class-off");
	        $(this).siblings().removeClass("class-on");
	        var tempUlId='#'+this.id+'-ul';
	        $(tempUlId).siblings().hide('fast',function(){
	        	$(tempUlId).show('fasfajiut');
	        });
		},
		changeButton_1:function(){
			//alert(this.className);
			if(this.className=="btn-2"){
				$(".bottom-slide-btn").animate({left:'50%'});
				startPoint= 0;
				commonObj.loadChart(1);
			}
			else if(this.className=="btn-1"){
				startPoint= 0;
				$(".bottom-slide-btn").animate({left:'0px'});
				commonObj.loadChart(0);
			}
			
		},
		changeImg:function(){
			//alert('feafe');
       		$("#num-class-p").text($(this).find("p").html());  
       		//alert($(this).find("span").css("background"));
       		//alert($(this).find("span").css('background-image'));
  		//alert('2');
            $("#num-class-icon").find('.icon').css({"background-image":$(this).find("span").css("background-image"),
                                     "background-repeat":"no-repeat",
                                     "background-size":"30px auto",
                                     "background-position":'0px 0px'});
            			
		},
		calculatorJsq:function() {
			//alert(this.id);
			var num=this.id;
			var screenObj = document.getElementById("screenName");
			var tempStr,tempValue,lastNum,str,len;		
		
			if(num=="eva"){
				if($(this).val()=="确认"){
					$("#calculator").hide();
					tempValue=$('#screenName').val();
				}			
				else{
					str=screenObj.value;
					lastNum=str.charAt(str.length-1);
					if((lastNum=="+")||(lastNum=="-")||(lastNum==".")){
						tempStr=str.substr(0,str.length-1);
						//alert(tempStr);
						tempValue=eval(tempStr);
						//$('#screenName').val(eval(tempStr));
					}
					else{
						tempValue=eval(str);
						//$('#screenName').val(eval(str));
					}
					$("#eva").val("确认");
					
				}
				
				$('#screenName').val(commonObj.twoAfter(tempValue));   
			}
			else if(num=="Back"){
				screenObj.value = screenObj.value.substring(0, screenObj.value.length - 1);
			}
   			else 
   			{   
   				str=screenObj.value;
   				lastNum=str.charAt(screenObj.value.length-1);
    			if((num==="+")||(num==="-")){
    				if((lastNum!="+")&&(lastNum!="-")&&(lastNum!=".")){
    					screenObj.value +=this.value;
    				}
   					$("#eva").val("=");
   				}
    			else if(num==='.'){
    				if((str!="0.00")&&(lastNum!="+")&&(lastNum!="-")&&(lastNum!=".")){
    					screenObj.value +=this.value;
    				}
    			}
    			else{
	    				if(str=="0.00"){
	    					screenObj.value='';
	    					screenObj.value +=this.value;
	    				}
	    				else if(screenObj.value.length>3){
							tempStr1=screenObj.value.substr(screenObj.value.length-3,3);//截取后三位
							if(tempStr1.indexOf('.')!=0){
								screenObj.value +=this.value;
							}
	    				}
	    				else{
	    					screenObj.value +=this.value;
	    				}
 						
    				
    			}
   			}
           $("#screenName").blur();
		},
		twoAfter:function(Str){
			    var Value;
			   // alert(Str.length);
				var tempStr=Str.substr(Str.length-3,3);//截取后三位				 
				//("two:"+tempStr);
				var len=tempStr.indexOf('.');	//alert(len);、、判断是否有’。‘
				if(len==0){Value=Str;}
				else if(len==1){Value=Str+'0';}
				else if(len==2){Value=Str+'00';}
				else if(len==-1){Value=Str+'.00';}	
				return Value;
		},
		DelObjValue:function(){
			//alert($(_this).find(".outClass").text());
			
			for(var i=0;i<outObjArry.length;i++){
 				if(outObjArry[i].name==$(_this).find(".outClass").text()){
 				outObjArry[i].value-=parseFloat($(_this).find(".outNum").text());
 				}
			}	
			for(var j=0;j<inObjArry.length;j++){
 				if(inObjArry[j].name==$(_this).find(".outClass").text()){
 				inObjArry[j].value-=parseFloat($(_this).find(".outNum").text());
 				}
			}	
		},
		addObjValue:function(name,value){
			//alert($(_this).find(".outClass").text());
			
			for(var i=0;i<outObjArry.length;i++){
 				if(outObjArry[i].name===name){
 				outObjArry[i].value+=parseFloat(value);
 				}
			}
			for(var j=0;j<inObjArry.length;j++){
 				if(inObjArry[j].name===name){
 				inObjArry[j].value+=parseFloat(value);
 				}
			}
			
			
		},
		Sum:function(type){
			var outSum=0,inSum=0;

			for(var l=0;l<outObjArry.length;l++){
 					outSum+=outObjArry[l].value;
 					console.log('outObjArry['+l+'],name:'+outObjArry[l].name);
 					console.log('outObjArry['+l+']:'+outObjArry[l].value);
 				
 				//	alert(outSum);
				}
			for(var k=0;k<inObjArry.length;k++){
 					inSum+=inObjArry[k].value;
 					console.log('inObjArry['+k+'],name:'+inObjArry[k].name);
 					console.log('inObjArry['+k+']:'+inObjArry[k].value);
 				//	alert(inObjArry[k].value);
			}
			for(var n=0;n<outObjArry.length;n++){
					outPctageObjArry[n]=outObjArry[n].value/outSum;
			}
			for(var m=0;m<inObjArry.length;m++){
					inPctageObjArry[m]=inObjArry[m].value/inSum;
			}
			
			if(type===0){
				return outSum;
			}
			else if(type===1){
				return inSum;
			}
			else{
				return outSum;
			}
		},
		chgAccountinglist:function(){
			var RmkMes=$("#remarkInput").val();
			var tempName=$("#num-class-p").text();
           	var tempvalue=$("#screenName").val();			
			$(_this).find('.icon').css({"background-image":$("#num-class-icon").find('.icon').css("background-image"),
                "background-repeat":"no-repeat",
                "background-size":"30px auto",
                "background-position":'0px 0px'});
            $(_this).find(".outClass").text(tempName);
            $(_this).find(".outMes").text(RmkMes=(RmkMes=="请输入备注信息")?'':RmkMes);
           	$(_this).find(".outNum").text(tempvalue);

           	commonObj.addObjValue(tempName,tempvalue);
           	
		},
		addChartlist:function(){
            var num=$("#screenName").val();
          //  alert("num"+num.length);
           	var categoryP=$("#num-class-p").text();
           	var BkgImg=$("#num-class-icon").find('.icon').css("background-image");
       //    	alert(BkgImg);
            var BkgRe='no-repeat';
            var BkgSize='30px auto';
            var BkgPos='0px 0px';
            var RmkMes=$("#remarkInput").val();
            RmkMes=(RmkMes=="请输入备注信息")?'':RmkMes;
            var chartliHtml='<li class="out-li">'+
	            '<div class="out-class-icon"><span class="icon" style=\'background-image: '+BkgImg+';'+
	            ' background-repeat:'+BkgRe+';'+'background-size:'+BkgSize+';'+'background-position: '+BkgPos+';\''+
	            '></span></div>'+
	            '<div class="out-class-mes "><p class="outClass">'+categoryP+'</p><p class="outMes">'+RmkMes+'</p>'+
	                '<p class="outNum">'+num+'</p>'+
	            '</div>'+           
				'</li>';
          	$(".chart-out-ul").append(chartliHtml);
        },
		addAccountinglist:function(){
            var num=$("#screenName").val();
          //  alert("num"+num.length);
           	var categoryP=$("#num-class-p").text();
           	var BkgImg=$("#num-class-icon").find('.icon').css("background-image");
       //    	alert(BkgImg);
            var BkgRe='no-repeat';
            var BkgSize='30px auto';
            var BkgPos='0px 0px';
            var RmkMes=$("#remarkInput").val();
            RmkMes=(RmkMes=="请输入备注信息")?'':RmkMes;
            var accountingliHtml='<li class="out-li">'+
	            '<div class="out-class-icon"><span class="icon" style=\'background-image: '+BkgImg+';'+
	            ' background-repeat:'+BkgRe+';'+'background-size:'+BkgSize+';'+'background-position: '+BkgPos+';\''+
	            '></span></div>'+
	            '<div class="out-class-mes "><p class="outClass">'+categoryP+'</p><p class="outMes">'+RmkMes+'</p>'+
	                '<p class="outNum">'+num+'</p>'+
	            '</div>'+           
				'</li>';
				/*空格是&nbsp*/
           // alert(accountingliHtml);
           	commonObj.addObjValue(categoryP,num);
          	$(".outlay-ul-0").append(accountingliHtml);
  
        },
        closeDetailPage:function(){
				var tempName=$(_this).find(".outClass").text();
				var tempValue=$(_this).find(".outNum").text();
				console.log('类别'+tempName);
				console.log('数值'+tempValue);
				commonObj.addObjValue(tempName,tempValue);
			
			commonObj.changePage('#account-page','#main-page');
			
        },
        createDetailPage:function(){
			_this=this;
			$("#chg-icon").removeClass("close-icon");
	       	$("#chg-icon").addClass("left-back-icon");

			$("#chg-title").html("账单详情");

			$("#del-btn").show();
			$('.footer').toggle();
			$("#screenName").blur(); 
          	$("#screenName").val($(_this).find(".outNum").text()); 
            $("#calculator").hide();
            $("#remarkInput").blur(); 
            var tempRemarkInput=$(_this).find(".outMes").text();
            tempRemarkInput==""?tempRemarkInpu="请输入备注信息":"";
           	$("#remarkInput").val(tempRemarkInpu); 
            $(".function-list").find("ul").hide();
            $("#num-class-icon").find('.icon').css({"background-image":$(this).find(".icon").css("background-image"),
                                  	 "background-repeat":"no-repeat",
                                     "background-size":"30px auto",
                                     "background-position":'0px 0px'});
            $("#num-class-p").text($(_this).find(".outClass").text());
            $("#account-out").addClass("class-on");
	       	$("#account-in").addClass("class-off");
	      	$("#account-out").removeClass("class-off");
	       	$("#account-in").removeClass("class-on");
	        $("#account-in-ul").hide('fast',function(){
	        $("#account-out-ul").show('fast');
	        });
			
			$("#main-page").hide('fast',function(){
			$("#account-page").show('fast');
			});
			//点击详情见面后删除当前的信息
			commonObj.DelObjValue();
		
		/*
        	_this=this;
  			$("#detailClass").text($(_this).find(".outClass").text());
        	$("#detailNum").text($(_this).find(".outNum").text());
           	
        	$("#detail-class-icon").css({"background-image":$(_this).find(".icon").css("background-image"),
                                     "background-repeat":"no-repeat",
                                     "background-size":"30px auto",
                                     "background-position":'0px 0px'});
        	$('#out-detail-page').show('fast',function(){
        		$("#main-page").hide('fast');
        		$("#footer").hide('fast');
        		      	
        	});
*/
        },
        delMes:function(){       	
	            var selfConfirm = new SelfWinsow({
	                slefTitle : "删除提醒",
	                types : "confirm",
	                selfInfo : typeof settings == "string" ? settings : "亲，真的删除该条流水么？",
	                selfOk : "删除",
	                selfNo : "取消",
	                callback:function(){//点击删除时的回调
	                $(_this).remove(commonObj.DelObjValue());
	                commonObj.changePage('#account-page','#main-page');
	               
	                }
	              });
	 
	          
        }
 		
 	
 
 
 
 
 
 
 
 	}
 
 
 
})